package com.atguigu.dga.governance.assessor.quality;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.dga.ds.bean.TDsTaskInstance;
import com.atguigu.dga.governance.assessor.Assessor;
import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Component("TIMELINESS")
public class TimelinessAssessor extends Assessor {

        //当日产出时效，超过前{days}天产出时效平均值{percent}%
        //  当日时效-平均时效   / 平均时效
       //  当日时效   submit date= assessdate    endtime-submitTime
       //  前{days}天平均时效    大于assessdate  小于 前n天date   endtime-submitTime 求和   求平均

        //则给0分，其余10分
    @Override
    public void checkProblem(AssessParam assessParam, GovernanceAssessDetail governanceAssessDetail) throws ParseException {
        String assessDate = assessParam.getAssessDate();
        //取参数
        String metricParamsJson = assessParam.getGovernanceMetric().getMetricParamsJson();
        JSONObject paramJsonObj = JSON.parseObject(metricParamsJson);
        int days = paramJsonObj.getInteger("days");

        //求前n天的日期
        Date assessDateDt = DateUtils.parseDate(assessDate, "yyyy-MM-dd");
        Date beforeDaysDate = DateUtils.addDays(assessDateDt, -days);
        String beforeDaysDateStr = DateFormatUtils.format(beforeDaysDate, "yyyy-MM-dd");

        List<TDsTaskInstance> tDsTaskInstanceList = assessParam.getTDsTaskInstanceList();
        //循环一次 获得当然的耗时  前n天总耗时
        Long  assessDateDurationMs=0L;
        Long  beforeDaysTotalDurationMs=0L;
        Long  beforeDaysCount=0L;
        for (TDsTaskInstance tDsTaskInstance : tDsTaskInstanceList) {
            String submitDt = DateFormatUtils.format(tDsTaskInstance.getSubmitTime(), "yyyy-MM-dd");
            if(submitDt.equals(assessDate)&&tDsTaskInstance.getState()==7){
                assessDateDurationMs= tDsTaskInstance.getEndTime().getTime()-tDsTaskInstance.getSubmitTime().getTime();
            }else if(submitDt.compareTo(assessDate)<0 && submitDt.compareTo(beforeDaysDateStr)>=0&&tDsTaskInstance.getState()==7){ //统计日期 小于考评日期 大于等于前n天日期
                beforeDaysTotalDurationMs += tDsTaskInstance.getEndTime().getTime()-tDsTaskInstance.getSubmitTime().getTime();
                beforeDaysCount++;
            }
        }
        // 平均时效
        long avgDurationMs = beforeDaysTotalDurationMs / beforeDaysCount;
        //  当日时效-平均时效   / 平均时效
        if(assessDateDurationMs>avgDurationMs ){
            BigDecimal realPercent = BigDecimal.valueOf(assessDateDurationMs).subtract(BigDecimal.valueOf(avgDurationMs)).divide(BigDecimal.valueOf(avgDurationMs)).movePointRight(2);
            //参数
            BigDecimal paramPercent = paramJsonObj.getBigDecimal("percent");
            if(realPercent.compareTo(paramPercent)>0){
                governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
                governanceAssessDetail.setAssessProblem("当日产出时效，超过前"+days+"天产出时效平均值"+paramPercent+"%, 实际超过"+realPercent+"%");
            }
        }

    }
}
